package com.arpa.wms.controller;

import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.dto.CloudprintStandardTemplatesDTO;
import com.arpa.wms.domain.entity.CloudprintStandardTemplates;
import com.arpa.wms.domain.vo.CloudprintStandardTemplatesVO;
import com.arpa.wms.service.ICloudprintStandardTemplatesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 */
@Api(tags = "云打印模板")
@RestController
@RequestMapping(value="/cloudprintStandardTemplates", produces = "application/json;charset=UTF-8")
@Log4j2
public class CloudprintStandardTemplatesController {
    @Autowired
    private ICloudprintStandardTemplatesService cloudprintStandardTemplatesService;

    /**
     * 获取 云打印模板 列表
     * @param cloudprintStandardTemplatesDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 云打印模板 列表", notes = "获取 云打印模板 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = CloudprintStandardTemplatesVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(CloudprintStandardTemplatesDTO cloudprintStandardTemplatesDTO, HttpServletRequest request) {
        try {
            CloudprintStandardTemplatesVO cloudprintStandardTemplatesVOSum = cloudprintStandardTemplatesService.queryListSum(cloudprintStandardTemplatesDTO);
            List<CloudprintStandardTemplatesVO> cloudprintStandardTemplatesVOList = cloudprintStandardTemplatesService.queryList(cloudprintStandardTemplatesDTO);

            Page returnPage = new Page(cloudprintStandardTemplatesDTO.getPageNum(), cloudprintStandardTemplatesDTO.getPageSize());
            returnPage.setRecords(cloudprintStandardTemplatesVOList);
            returnPage.setTotal(cloudprintStandardTemplatesVOSum.getTotal());
            returnPage.setSumObject(cloudprintStandardTemplatesVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "获取 云打印模板 详情", notes = "获取 云打印模板 详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = CloudprintStandardTemplatesVO.class))
    @NeedLogin
    @GetMapping("/details/{id}")
    public Result details(@PathVariable Integer id){
        if (id == null || id == 0){
            return Result.error("id不能为空");
        }
        CloudprintStandardTemplates cloudprintStandardTemplatesServiceOne = cloudprintStandardTemplatesService
                .getOne(new QueryWrapper<CloudprintStandardTemplates>().lambda().eq(CloudprintStandardTemplates::getId, id));
        if (cloudprintStandardTemplatesServiceOne != null){
            return Result.ok().data(cloudprintStandardTemplatesServiceOne);
        }
        return Result.error("没有查询到id"+id+"的模板详情");
    }


    @ApiOperation(value = "获取 云打印模板 ", notes = "获取 云打印模板 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = CloudprintStandardTemplatesVO.class))
    @NeedLogin
    @GetMapping("listTemplates")
    public Result listTemplates(CloudprintStandardTemplatesDTO cloudprintStandardTemplatesDTO, HttpServletRequest request) {
        try {
            List<CloudprintStandardTemplates> cloudprintStandardTemplatesList = cloudprintStandardTemplatesService.listTemplates(cloudprintStandardTemplatesDTO);
            return Result.ok().data(cloudprintStandardTemplatesList);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "添加 云打印模板", notes = "添加 云打印模板 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result save(@Validated(CloudprintStandardTemplates.Save.class) @RequestBody CloudprintStandardTemplates entity, HttpServletRequest request){
        try {
            if(StringUtils.isBlank(entity.getStandardTemplateUrl())){
                return Result.error("标准模板url不能为空");
            }
            if(entity.getStandardWaybillType() == null || entity.getStandardWaybillType() == 0){
                return Result.error("模板类型不能为空");
            }
            cloudprintStandardTemplatesService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }

    }

    @ApiOperation(value = "修改 云打印模板", notes = "修改 云打印模板 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(@Validated(CloudprintStandardTemplates.Update.class) @RequestBody CloudprintStandardTemplatesDTO entity, HttpServletRequest request){
        if(entity.getCloudprintStandardTemplatesId() == null || entity.getCloudprintStandardTemplatesId() == 0 ){
            return Result.error("id不能为空");
        }
        int updateNum = cloudprintStandardTemplatesService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getId());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    @ApiOperation(value = "删除 云打印模板", notes = "删除 云打印模板 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids, HttpServletRequest request){
        if (ids ==null || ids.size() == 0){
            return Result.error("code不能为空");
        }
        int deleteNum = cloudprintStandardTemplatesService.delete(ids);
        if (deleteNum > 0) {
            return Result.ok("删除成功,删除"+deleteNum+"条数据！");
        }else {
            return Result.error("删除失败，删除"+deleteNum+"条数据");
        }
    }


}
